﻿namespace Bll.Procurement
{
    using Bll.Company;
    using Bll.Stock;
    using IDal.Procurement;
    using Model.Company;
    using Model.Procurement;
    using Model.Systems;
    using System;
    using System.Data;
    using WinFormSession;
    using DALFactory;
    public class ProcurementBll
    {
        private static readonly IProcurement dal = DataAccess.CreateProcurement();

        public static void Add(ProcurementEnt model)
        {
            dal.Add(model);
        }

        public static void Delete(int ID)
        {
            dal.Delete(ID);
        }

        public static bool FuCheck(ProcurementEnt model)
        {
            return dal.FuCheck(model);
        }

        public static DataTable GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }

        public static int GetMaxId(string swhere)
        {
            return dal.GetMaxId(swhere);
        }

        public static ProcurementEnt GetModel(int ID)
        {
            return dal.GetModel(ID);
        }

        public static DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            return dal.GetPageList(PageSize, PageIndex, strWhere);
        }

        public static bool InvalidDocuments(ProcurementEnt model)
        {
            bool flag;
            try
            {
                DataTable list = ProcurementPaymentListBll.GetList("ProcurementId=" + model.ProcurementId);
                if (list.Rows.Count > 0)
                {
                    string str = list.Rows[0]["PaymentListId"].ToString();
                    string str2 = list.Rows[0]["PayMentMoney"].ToString();
                    ProcurementPaymentEnt ent = ProcurementPaymentBll.GetModel(int.Parse(list.Rows[0]["PurchaseId"].ToString()));
                    if ((ent != null) && (ent.IsClear == 1))
                    {
                        UserEnt ent2 = (UserEnt) WinformSession.GetSession()["user"];
                        CompayAccountEnt ent3 = CompayAccountBll.GetModel(ent.AccountId);
                        ent3.AccountMoney += ent.AllMoney;
                        CompayAccountBll.Update(ent3);
                        CompayAccountDetailsEnt ent4 = new CompayAccountDetailsEnt {
                            CheckMan = ent2.UserId,
                            CheckTime = DateTime.Now,
                            BuildMoney = ent.AllMoney,
                            CreateMan = ent2.UserId,
                            CreateTime = DateTime.Now,
                            DetailsType = 3,
                            InputDetailsSource = ent.AccountId,
                            IsCheck = 1,
                            NotesNum = ent.PuDocuNum,
                            Remark = ent.Remark,
                            ChainNum = ent2.ChainNum
                        };
                        CompayAccountDetailsBll.Add(ent4);
                        ent4.DetailsId = CompayAccountDetailsBll.GetMaxId("ChainNum=" + ent2.ChainNum);
                        CompayAccountDetailsBll.Check(ent4);
                    }
                    if (StockBll.ProReturn(model.ProcurementId) > 0)
                    {
                        return false;
                    }
                    model.IsBlackOut = 1;
                    UserEnt ent5 = (UserEnt) WinformSession.GetSession()["user"];
                    model.BlackManId = ent5.UserId;
                    model.BlackManName = ent5.UserName;
                    model.BlackTime = DateTime.Now;
                    UpdateZuoFei(model);
                    return true;
                }
                flag = false;
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return flag;
        }

        public static void Update(ProcurementEnt model)
        {
            dal.Update(model);
        }

        public static void UpdateZuoFei(ProcurementEnt model)
        {
            dal.UpdateZuoFei(model);
        }
    }
}

